/*----------------------------------------------------------------------------*\
|
|						Public Interface to Muse Technology
|
|							     www.Muse.com
| Author : SunJun 
| Email : sundoom@msn.com
\*----------------------------------------------------------------------------*/

//////////////////////////////////////////////////////////////////////////
// $Id: MReference.h,v 1.1 2009/04/03 12:46:09 sunjun Exp $
//////////////////////////////////////////////////////////////////////////

#ifndef __MReference__
#define __MReference__

//////////////////////////////////////////////////////////////////////////

#if defined __cplusplus

//////////////////////////////////////////////////////////////////////////

#include <Muse.h>
#include <MPtr.h>
#include <MMutex.h>

//////////////////////////////////////////////////////////////////////////

NAMESPACE_BEGIN

//////////////////////////////////////////////////////////////////////////	

	// base class from providing referencing counted objects.
	class API MReference
	{
	public:
		MReference();		

		// increment the reference count by one, indicating that 
		// this object has another pointer which is referencing it.
		INLINE VOID ref() const;
    
		// decrement the reference count by one, indicating that 
		// a pointer to this object is referencing it.  If the
		// reference count goes to zero, it is assumed that this object
		// is no longer referenced and is automatically deleted.
		INLINE VOID unref() const;
    
		// return the number pointers currently referencing this object.
		INLINE I32 count() const;

		// set whether to use a mutex to ensure ref() and unref() are thread safe.
		virtual VOID safe( BOOL is_safe );

		// get the mutex used to ensure thread safety of ref()/unref().
		INLINE MMutex* mutex() const;

	protected:
		virtual ~MReference();
	
	private:	
		mutable class MMutex* mRefMutex;	
		mutable I32	mRefCount;
	};

	typedef MPtr<MReference>MReferencePtr;

//////////////////////////////////////////////////////////////////////////

NAMESPACE_END

//////////////////////////////////////////////////////////////////////////

#endif // __cplusplus

//////////////////////////////////////////////////////////////////////////

#endif // __MReference__

//////////////////////////////////////////////////////////////////////////
